<template>
  <el-dialog
    :title="!dataForm.Id ? '新增租户' : '修改租户'"
    :close-on-click-modal="false"
    width="30%"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item size="mini" label="租户名称" prop="Name">
        <el-input v-model="dataForm.Name" maxlength="25" show-word-limit placeholder="请输入租户名称" />
      </el-form-item>
      <el-form-item size="mini" label="租户代码" prop="Code">
        <el-input v-model="dataForm.Code" maxlength="25" show-word-limit placeholder="请输入租户代码" />
      </el-form-item>
      <el-row>
        <el-col style="text-align: right;">
          <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
          <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { getTenant, saveTenant, updateTenant } from '@/api/tenant'
export default {
  data() {
    return {
      dataForm: {
        Id: '',
        Code: '',
        Name: ''
      },
  			dataRule: {
  				Name: [
  					{ required: true, message: '租户名称不能为空', trigger: 'blur' }
  				],
  				Code: [
  					{ required: true, message: '租户代码不能为空', trigger: 'blur' }
  				]
  			},
      visible: false
    }
  },
  methods: {
    init(id) {
	    this.dataForm.Id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (id && id > 0) {
        getTenant(id).then((data) => {
          if (data && data.code === '20000') {
            this.dataForm.Id = data.tenant.Id
            this.dataForm.Code = data.tenant.Code
            this.dataForm.Name = data.tenant.Name
          }
        })
      }
    },
    dataFormSubmit() {
	      this.$refs['dataForm'].validate((valid) => {
	        if (valid) {
	          var data = {
	            'Id': this.dataForm.Id || undefined,
	            'Code': this.dataForm.Code,
	            'Name': this.dataForm.Name
	          }
	          var permission
	          if (this.dataForm.Id && this.dataForm.Id > 0) {
	            permission = updateTenant(data)
	          } else {
	            permission = saveTenant(data)
	          }
	          permission.then((data) => {
	            if (data && data.code === '20000') {
	              this.$message({
	                message: '操作成功',
	                type: 'success',
	                duration: 1500,
	                onClose: () => {
	                  this.visible = false
	                  this.$emit('refreshDataList')
	                }
	              })
	            } else {
	              this.$message.error(data.msg)
	            }
	          })
	        }
	      })
    }
  }
}
</script>
